<template>
    <el-aside width="220px" class="imageAside" v-loading="loading">
        <div class="imageAsideTop">
            <AsideList @delete="handleDelete(item.id)" @edit="handleEdit(item)" v-for="(item, index) in list" :key="item.id"
                :active="activeId === item.id" @click="handleChangeActiveId(item.id)">
                {{ item.name }}
            </AsideList>
        </div>
        <div class="imageAsideBottom">
            <el-pagination background layout="prev, next" :total="total" :current-page="currentPage" :page-size="limit"
                @current-change="getData" />
        </div>
    </el-aside>
    <FormDrawer ref="formDrawerRef" @submit="handleSubmit" :title="drawerTitle">
        <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline=false>
            <el-form-item label="分类名称" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="order">
                <el-input-number v-model="form.order" :min="0" :max="1000" />
            </el-form-item>
        </el-form>
    </FormDrawer>
</template>
<script setup>
import { ref, reactive, computed } from 'vue';
import FormDrawer from '@/components/FormDrawer.vue';
import AsideList from './components/AsideList.vue';
import { getImageClassList, createImageClassList, updateImageClassList, deleteImageClassList } from '@/api/image.js';
import { elNotification } from '@/hooks/elNotification'


// 加载动画
const loading = ref(false)
const list = ref([])

// 分页
const currentPage = ref(1)
const total = ref(0)
const limit = ref(10)

// 获取数据
function getData(p = null) {
    if (typeof p == "number") {
        currentPage.value = p
    }
    loading.value = true
    getImageClassList(currentPage.value).then(res => {
        list.value = res.list
        total.value = res.totalCount
        let item = list.value[0]
        if (item) {
            handleChangeActiveId(item.id)
        }
    }).finally(() => {
        loading.value = false
    })
}
getData()

// 抽屉
const editId = ref(0)
const drawerTitle = computed(() => editId.value ? '修改' : '新增')
const formDrawerRef = ref(null)

// 提交抽屉
const handleSubmit = () => {
    formRef.value.validate((valid) => {
        if (!valid) return

        formDrawerRef.value.showLoading()

        const fun = editId.value ? updateImageClassList(editId.value, form) : createImageClassList(form)
        fun.then(() => {
            elNotification(drawerTitle + "成功")
            getData(editId.value ? currentPage.value : 1)
            formDrawerRef.value.close()
        }).finally(() => {
            formDrawerRef.value.hideLoading()
        })
    })
}

const form = reactive({
    name: '',
    order: 50
})

const formRef = ref(null)

const rules = reactive({
    name: [{
        required: true,
        message: '分类名称不能为空',
        trigger: 'blur'
    }],
    order: [{
        required: true,
        message: '排序不能为空',
        trigger: 'blur'
    }]
})

// 新增
const handleCreate = () => {
    editId.value = 0
    form.name = ''
    form.order = 50
    formDrawerRef.value.open()
}

// 编辑
const handleEdit = (row) => {
    editId.value = row.id
    form.name = row.name
    form.order = row.order
    formDrawerRef.value.open()
}

//删除
const handleDelete = (id) => {
    loading.value = true
    deleteImageClassList(id).then(res => {
        elNotification("删除成功")
        getData()
    }).finally(() => {
        loading.value = false
    })
}

// 选中图库分类ID
let activeId = ref()
const emit = defineEmits(['change'])

// 切换分类
function handleChangeActiveId(id) {
    activeId.value = id
    emit('change', id)
}

defineExpose({
    handleCreate
})

</script>
<style scoped lang="less">
.imageAside {
    position: relative;
    border-right: 1px solid #eee;

    .imageAsideTop {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 50px;
        overflow-y: auto;
    }

    .imageAsideBottom {
        position: absolute;
        bottom: 0;
        height: 50px;
        left: 0;
        right: 0;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}
</style>